package cn.stylefeng.guns.modular.demo.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.roses.kernel.auth.api.LoginUserApi;
import cn.stylefeng.roses.kernel.auth.api.context.LoginContext;
import cn.stylefeng.roses.kernel.auth.api.pojo.login.LoginUser;
import cn.stylefeng.roses.kernel.rule.exception.base.ServiceException;
import cn.stylefeng.guns.modular.demo.entity.FixedAssetsInformation;
import cn.stylefeng.guns.modular.demo.enums.FixedAssetsInformationExceptionEnum;
import cn.stylefeng.guns.modular.demo.mapper.FixedAssetsInformationMapper;
import cn.stylefeng.guns.modular.demo.pojo.request.FixedAssetsInformationRequest;
import cn.stylefeng.guns.modular.demo.service.FixedAssetsInformationService;
import cn.stylefeng.roses.kernel.db.api.factory.PageFactory;
import cn.stylefeng.roses.kernel.db.api.factory.PageResultFactory;
import cn.stylefeng.roses.kernel.db.api.pojo.page.PageResult;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Date;

/**
 * 固定资产信息表业务实现层
 *
 * @author admin
 * @date 2022/11/19 18:06
 */
@Service
public class FixedAssetsInformationServiceImpl extends ServiceImpl<FixedAssetsInformationMapper, FixedAssetsInformation> implements FixedAssetsInformationService {

	@Override
    public void add(FixedAssetsInformationRequest fixedAssetsInformationRequest) {
        FixedAssetsInformation fixedAssetsInformation = new FixedAssetsInformation();
        BeanUtil.copyProperties(fixedAssetsInformationRequest, fixedAssetsInformation);
        // 获取当前登录人信息
        LoginUserApi me = LoginContext.me();
        LoginUser loginUser = me.getLoginUser();
        // 新增初始化
        fixedAssetsInformation.setDelFlag("N");
        fixedAssetsInformation.setCreatorId(loginUser.getUserId());
        fixedAssetsInformation.setCreatoUser(loginUser.getSimpleUserInfo().getRealName());
        fixedAssetsInformation.setCreatedTime(new Date());
        fixedAssetsInformation.setUpdateId(loginUser.getUserId());
        fixedAssetsInformation.setUpdateUser(loginUser.getSimpleUserInfo().getRealName());
        fixedAssetsInformation.setUpdateTime(new Date());
        //默认状态为已归还
        fixedAssetsInformation.setState("Y");
        this.save(fixedAssetsInformation);
    }

    @Override
    public void del(FixedAssetsInformationRequest fixedAssetsInformationRequest) {
        FixedAssetsInformation fixedAssetsInformation = this.queryFixedAssetsInformation(fixedAssetsInformationRequest);
        BeanUtil.copyProperties(fixedAssetsInformationRequest, fixedAssetsInformation);
        fixedAssetsInformation.setDelFlag("Y");
        this.updateById(fixedAssetsInformation);
    }

    @Override
    public void edit(FixedAssetsInformationRequest fixedAssetsInformationRequest) {
        FixedAssetsInformation fixedAssetsInformation = this.queryFixedAssetsInformation(fixedAssetsInformationRequest);
        BeanUtil.copyProperties(fixedAssetsInformationRequest, fixedAssetsInformation);
        // 获取当前登录人信息
        LoginUserApi me = LoginContext.me();
        LoginUser loginUser = me.getLoginUser();
        // 新增初始化
        fixedAssetsInformation.setUpdateId(loginUser.getUserId());
        fixedAssetsInformation.setUpdateUser(loginUser.getSimpleUserInfo().getRealName());
        fixedAssetsInformation.setUpdateTime(new Date());
        this.updateById(fixedAssetsInformation);
    }

    @Override
    public FixedAssetsInformation detail(FixedAssetsInformationRequest fixedAssetsInformationRequest) {
        return this.queryFixedAssetsInformation(fixedAssetsInformationRequest);
    }

    @Override
    public PageResult<FixedAssetsInformation> findPage(FixedAssetsInformationRequest fixedAssetsInformationRequest) {
        LambdaQueryWrapper<FixedAssetsInformation> wrapper = createWrapper(fixedAssetsInformationRequest);
        wrapper.in(FixedAssetsInformation::getDelFlag,"N");
        // 借出时间
        if (ObjectUtil.isNotNull(fixedAssetsInformationRequest.getLendTime())){
            String[] split = fixedAssetsInformationRequest.getLendTime().split(",");
            wrapper.between(FixedAssetsInformation::getLendTime,split[0],split[1]);
        }

        // 归还时间
        if (ObjectUtil.isNotNull(fixedAssetsInformationRequest.getReturnTime())){
            String[] split = fixedAssetsInformationRequest.getReturnTime().split(",");
            wrapper.between(FixedAssetsInformation::getReturnTime,split[0],split[1]);
        }
        Page<FixedAssetsInformation> sysRolePage = this.page(PageFactory.defaultPage(), wrapper);
        return PageResultFactory.createPageResult(sysRolePage);
    }

    @Override
    public List<FixedAssetsInformation> findList(FixedAssetsInformationRequest fixedAssetsInformationRequest) {
        LambdaQueryWrapper<FixedAssetsInformation> wrapper = this.createWrapper(fixedAssetsInformationRequest);
        wrapper.in(FixedAssetsInformation::getDelFlag,"N");
        return this.list(wrapper);
    }

    /**
     * 获取信息
     *
     * @author admin
     * @date 2022/11/19 18:06
     */
    private FixedAssetsInformation queryFixedAssetsInformation(FixedAssetsInformationRequest fixedAssetsInformationRequest) {
        FixedAssetsInformation fixedAssetsInformation = this.getById(fixedAssetsInformationRequest.getId());
        if (ObjectUtil.isEmpty(fixedAssetsInformation)) {
            throw new ServiceException(FixedAssetsInformationExceptionEnum.FIXED_ASSETS_INFORMATION_NOT_EXISTED);
        }
        return fixedAssetsInformation;
    }

    /**
     * 创建查询wrapper
     *
     * @author admin
     * @date 2022/11/19 18:06
     */
    private LambdaQueryWrapper<FixedAssetsInformation> createWrapper(FixedAssetsInformationRequest fixedAssetsInformationRequest) {
        LambdaQueryWrapper<FixedAssetsInformation> queryWrapper = new LambdaQueryWrapper<>();

        Long id = fixedAssetsInformationRequest.getId();
        String name = fixedAssetsInformationRequest.getName();
        String state = fixedAssetsInformationRequest.getState();
        String memo = fixedAssetsInformationRequest.getMemo();
        String lendUserName = fixedAssetsInformationRequest.getLendUserName();
        String lendTime = fixedAssetsInformationRequest.getLendTime();
        String returnTime = fixedAssetsInformationRequest.getReturnTime();
        String delFlag = fixedAssetsInformationRequest.getDelFlag();

        queryWrapper.eq(ObjectUtil.isNotNull(id), FixedAssetsInformation::getId, id);
        queryWrapper.like(ObjectUtil.isNotEmpty(name), FixedAssetsInformation::getName, name);
        queryWrapper.like(ObjectUtil.isNotEmpty(state), FixedAssetsInformation::getState, state);
        queryWrapper.like(ObjectUtil.isNotEmpty(memo), FixedAssetsInformation::getMemo, memo);
        queryWrapper.eq(ObjectUtil.isNotNull(lendUserName), FixedAssetsInformation::getLendUserName, lendUserName);
        queryWrapper.eq(ObjectUtil.isNotNull(lendTime), FixedAssetsInformation::getLendTime, lendTime);
        queryWrapper.eq(ObjectUtil.isNotNull(returnTime), FixedAssetsInformation::getReturnTime, returnTime);
        queryWrapper.like(ObjectUtil.isNotEmpty(delFlag), FixedAssetsInformation::getDelFlag, delFlag);

        return queryWrapper;
    }


    @Override
    public void lend(FixedAssetsInformationRequest fixedAssetsInformationRequest) {
        FixedAssetsInformation fixedAssetsInformation = this.queryFixedAssetsInformation(fixedAssetsInformationRequest);
        BeanUtil.copyProperties(fixedAssetsInformationRequest, fixedAssetsInformation);
        //默认状态未归还
        fixedAssetsInformation.setState("N");
        // 获取当前登录人信息
        LoginUserApi me = LoginContext.me();
        LoginUser loginUser = me.getLoginUser();
        fixedAssetsInformation.setLendUserName(loginUser.getSimpleUserInfo().getRealName());
        fixedAssetsInformation.setLendTime(new Date());
        fixedAssetsInformation.setUpdateId(loginUser.getUserId());
        fixedAssetsInformation.setUpdateUser(loginUser.getSimpleUserInfo().getRealName());
        fixedAssetsInformation.setUpdateTime(new Date());
        this.updateById(fixedAssetsInformation);
    }

    @Override
    public void back(FixedAssetsInformationRequest fixedAssetsInformationRequest) {
        FixedAssetsInformation fixedAssetsInformation = this.queryFixedAssetsInformation(fixedAssetsInformationRequest);
        BeanUtil.copyProperties(fixedAssetsInformationRequest, fixedAssetsInformation);
        //默认状态未归还
        fixedAssetsInformation.setState("Y");
        // 获取当前登录人信息
        LoginUserApi me = LoginContext.me();
        LoginUser loginUser = me.getLoginUser();
        fixedAssetsInformation.setReturnTime(new Date());
        fixedAssetsInformation.setUpdateId(loginUser.getUserId());
        fixedAssetsInformation.setUpdateUser(loginUser.getSimpleUserInfo().getRealName());
        fixedAssetsInformation.setUpdateTime(new Date());
        this.updateById(fixedAssetsInformation);
    }
}